//渲染
//    let managementpageSize = 5; //每页显示条数
//   let managementcurrentPage = 1; //当前页
var storeguanli = GetUser("storeguanli");
var management = GetUser("management");
function xuanran(management,managementpageSize,managementcurrentPage) {
  let start = (managementcurrentPage - 1) * managementpageSize;
  let end = managementcurrentPage * managementpageSize;
  $("#userTable").empty();
  for (var i = start; i < end && i < management.length; i++) {
    tr = `
        <tr class="line">
        <td><input type="checkbox" data-id="${
          management[i].id
        }" class="cbxAll" ></td>
        <td>${i + 1}</td>
        <td>${management[i].managementname}</td>
        <td>${management[i].leixing}</td>
        <td>${management[i].guangwangip}</td>
        <td>${management[i].jingxuan}</td>
        <td>${management[i].jinyong}</td>
        <td >
            <!-- 修改图标 -->
            <button data-id="${
              management[i].id
            }" type="button" class="layui-btn layui-btn-sm xiugai" ><i class="layui-icon"></i></button>
            <!-- 删除图标 -->
            <button data-id="${
              management[i].id
            }" type="button" class="cc layui-btn layui-btn-normal layui-btn-sm " style="background-color: red;">
                <i class="layui-icon"></i></button>
        </td>
    </tr>
    `;
    $("#userTable").append(tr);
  }
}
BindPage(management);
AllChoose();

//页数
function BindPage(Query) {
  let managementpageSize = 5; //每页显示条数
  let managementcurrentPage = 1; //当前页
  let TotalPage = Math.ceil(Query.length / managementpageSize);
  if (managementcurrentPage > TotalPage) {
    managementcurrentPage = TotalPage;
  }
  if (TotalPage == 0) {
    managementcurrentPage = 1;
  }
  //生成对应页码
  $("#divPage").html("");
  for (var i = 1; i <= TotalPage; i++) {
    $("#divPage").append(`<button class="pageBut">${i}</button>`);
    $(".pageBut").eq(managementcurrentPage-1).addClass("cyan").siblings().removeClass("cyan");
    xuanran(management,managementpageSize,managementcurrentPage);
  }
  AddPageEvent();

  //页码绑定事件
function AddPageEvent() {
  $("#divPage").on("click", ".pageBut", function () {
    managementcurrentPage = parseInt($(this).text());
    $(this).addClass("cyan").siblings().removeClass("cyan");
    xuanran(management,managementpageSize,managementcurrentPage);
  });
}
}
// //页码绑定事件
// function AddPageEvent() {
//     // let managementpageSize = 5; //每页显示条数
//     // let managementcurrentPage = 1; //当前页
//   $("#divPage").on("click", ".pageBut", function () {
//     managementcurrentPage = parseInt($(this).text());
//     console.log(managementcurrentPage);
//     $(this).addClass("cyan").siblings().removeClass("cyan");
//     xuanran(management,managementpageSize,managementcurrentPage);
//   });
// }
function Addstore() {
  let id = Date.now();
  let managementname = $("#managementname").val();
  let leixing = $("#leixing").val();
  let guangwangip = $("#guangwangip").val();
  let jingxuan = $("#jingxuan").val();
  let jinyong = $("#jinyong").val();
  management.push({
    id: id,
    managementname: managementname,
    leixing: leixing,
    guangwangip: guangwangip,
    jingxuan: jingxuan,
    jinyong: jinyong,
  });
}
//添加绑定事件
$("#addbtn").click(function () {
  $(".modalAdd").show();
  $("#show").css("display", "none");
  $("#tijiao").off("click");
  $("#tijiao").click(function () {
    layer.confirm(
      "确认提交？",
      {
        btn: ["确认", "取消"], //按钮
      },
      function () {
        $(".modalAdd").hide();
        $("#show").css("display", "block");
        Addstore();
        BindPage(management);
        // AddPageEvent();
        layer.msg("添加成功！", { icon: 1, time: 500 });
      }
    );
  });
  $("#quxiao").click(function () {
    $(".modalAdd").hide();
    $("#show").css("display", "block");
  });
  $("#fanhui").click(function () {
    $(".modalAdd").hide();
    $("#show").css("display", "block");
  });
});

//删除
$("#userTable").on("click", ".cc", function () {
  let id = $(this).attr("data-id");
  layer.confirm(
    "确认删除？",
    {
      title: "删除",
      btn: ["确认", "取消"],
    },
    function () {
        let managementpageSize = 5; //每页显示条数
        let TotalPage = Math.ceil(management.length / managementpageSize);
        for(let i=0;i<TotalPage;i++){
            let Addstorea=$(".pageBut").eq(i).attr("class");
            let Addas=Addstorea.split(" ");
            if(Addas[1]=="cyan") {
               var pagesname= $(".pageBut").eq(i).html();
            }
        //    console.log(Addas); 
        }
        
      let newuser = management.filter(function (item) {
        return item.id != id;
      });
      management = newuser;
      BindPage(management);
      addUser("management",management)
      xuanran(management,managementpageSize,pagesname);
      layer.msg("已删除", { icon: 1, time: 500 });
    },
    function () {
      layer.msg("已取消", { icon: 2, time: 500 });
    }
  );
});
//全选
function AllChoose() {
  $(".All").click(function () {
    let choose = $(".All").prop("checked");
    $(".cbxAll").prop("checked", choose);
  });
}
//批量删除
$("#BtnAll").click(function () {
    let managementpageSize = 5; //每页显示条数
    let TotalPage = Math.ceil(management.length / managementpageSize);
    for(let i=0;i<TotalPage;i++){
        let Addstorea=$(".pageBut").eq(i).attr("class");
        let Addas=Addstorea.split(" ");
        if(Addas[1]=="cyan") {
           var pagesname= $(".pageBut").eq(i).html();
        }
    //    console.log(Addas); 
    }
  var arr = $(".cbxAll:checked").get();
  layer.confirm(
    "确认全部删除？",
    {
      btn: ["确认", "取消"],
    },
    function () {
      arr.forEach((v) => {
        management.forEach((el, i, m) => {
          if (el.id == $(v).attr("data-id")) {
            m.splice(i, 1);
          }
        });
      });
      $(".All").prop("checked", false);
      // AddPageEvent();
      BindPage(management);
      addUser("management",management)
      xuanran(management,managementpageSize,pagesname);
      layer.msg("已删除", {
        icon: 1,
        time: 500,
      });
    },
    function () {
      layer.msg("已取消", {
        icon: 2,
        time: 500,
      });
    }
  );
});
//编辑

function Addmanagement(id){
  var managementname = $(".managementname").val();
  var leixing = $(".leixing").val();
  var guangwangip = $(".guangwangip").val();
  var jingxuan = $(".jingxuan").val();
  var jinyong = $(".jinyong").val();
  for (var i = 0; i < management.length; i++) {
    if (id == management[i].id) {
      management[i].id = id;
      management[i].managementname = managementname;
      management[i].leixing = leixing;
      management[i].guangwangip = guangwangip;
      management[i].jingxuan = jingxuan;
      management[i].jinyong = jinyong;
      $("input:text").val("");
    }
}
}
$("#userTable").on("click", ".xiugai", function () {
  let managementpageSize = 5; //每页显示条数
    let TotalPage = Math.ceil(management.length / managementpageSize);
    for(let i=0;i<TotalPage;i++){
        let Addstorea=$(".pageBut").eq(i).attr("class");
        let Addas=Addstorea.split(" ");
        if(Addas[1]=="cyan") {
           var pagesname= $(".pageBut").eq(i).html();
        }
    }
  var id = $(this).attr("data-id");
  var obj = management.find((v) => v.id == id);
  $(".id").val(obj.id);
  $(".managementname").val(obj.managementname);
  $(".leixing").val(obj.leixing);
  $(".guangwangip").val(obj.guangwangip);
  $(".jingxuan").val(obj.jingxuan);
  $(".jinyong").val(obj.jinyong);
  xuanran(management,5,1);
// $(".xiugai").click(function () {
    
  $(".modalUpdate").show();
  $("#show").css("display", "none");
  $("#tijiao1").off("click");
  $("#tijiao1").click(function () {
    layer.confirm(
      "确认提交？",{
        btn: ["确认", "取消"], //按钮
      },function (){
       $(".modalUpdate").hide();
        $("#show").css("display", "block");
        BindPage(management);
        Addmanagement(id)
        // AddPageEvent();
        addUser("management",management)
        xuanran(management,managementpageSize,pagesname);
        layer.msg("提交成功", { icon: 1, time: 500 });
        });
  })
  $("#quxiao1").click(function () {
    $(".modalUpdate").hide();
    $("#show").css("display", "block");
  });
  $("#fanhui1").click(function () {
    $(".modalUpdate").hide();
    $("#show").css("display", "block");
  });
});
//查询
$("#queryMem").on("click", function () {
    let managementpageSize = 5; //每页显示条数
    let TotalPage = Math.ceil(management.length / managementpageSize);
    for(let i=0;i<TotalPage;i++){
        let Addstorea=$(".pageBut").eq(i).attr("class");
        let Addas=Addstorea.split(" ");
        if(Addas[1]=="cyan") {
           var pagesname= $(".pageBut").eq(i).html();
        }
    //    console.log(Addas); 
    }
  Query = [];
  var text1 = $("#text1").val().trim();
  var text2 = $("#text2").val().trim();
  if (!text1 && !text2) return xuanran(parent.management);
  Query = parent.management.filter((chaxun) => {
    if (chaxun.managementname == text1 || chaxun.leixing == text2) {
      return chaxun;
    }
  });
  BindPage(Query);
  // AddPageEvent();

  xuanran(Query,managementpageSize,pagesname);
});
$("#userTable").on("mouseover", ".line", function () {
  $(this).css("background-color", "#e8e8e8");
  $("#userTable").on("mouseout", ".line", function () {
    $(this).css("background-color", "white");
  });
});


function pagenumerevent(pagenuber) {
    $(pagenuber).click(function() {
        let managementpageSize = 5; //每页显示条数
        let TotalPage = Math.ceil(management.length / managementpageSize);
        for(let i=0;i<TotalPage;i++){
            let Addstorea=$(".pageBut").eq(i).attr("class");
            let Addas=Addstorea.split(" ");
            if(Addas[1]=="cyan") {
               var pagesname= $(".pageBut").eq(i).html();
            }
        //    console.log(Addas); 
        }
        if (pagenuber == '#pagDow') {
            pagesname--;
            for (let i = 0; i < TotalPage; i++) {
                let buttoncontent = parseInt($('.pageBut').eq(i).text());
                if (buttoncontent == pagesname) {
                    $('.pageBut').eq(i).addClass("cyan").siblings().removeClass("cyan");
                }
            }
        } else if (pagenuber == '#pagUp') {
            pagesname++;
            for (let i = 0; i < TotalPage; i++) {
                let buttoncontent = parseInt($('.pageBut').eq(i).text());
                if (buttoncontent == pagesname) {
                    $('.pageBut').eq(i).addClass("cyan").siblings().removeClass("cyan");
                }
            }
        }
        if (pagesname <= 0) {
            pagesname = 1;
            $('.pageBut').eq(0).addClass("cyan").siblings().removeClass("cyan");
        } else if (pagesname >= TotalPage) {
            pagesname = TotalPage;
        }
        xuanran(management,managementpageSize,pagesname);
    })
}
pagenumerevent('#pagUp');//上一页
pagenumerevent('#pagDow');//下一页
